package gateway

import (
	"github.com/gorilla/mux"
	"net/http"
)

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes{
	Route{
		Name: "ListMoon", Method: http.MethodGet, Pattern: "/moon", HandlerFunc: ListMoon,
	},
	Route{
		Name: "AddMoon", Method: http.MethodPost, Pattern: "/moon/add", HandlerFunc: AddMoon,
	},
	Route{
		Name: "ListMoonContainers", Method: http.MethodGet, Pattern: "/moon/{id}/containers", HandlerFunc: ListMoonContainers,
	},
	Route{
		Name: "DeleteMoon", Method: http.MethodDelete, Pattern: "/moon/{id}", HandlerFunc: DeleteMoon,
	},
	Route{
		Name: "InspectMoon", Method: http.MethodGet, Pattern: "/moon/{id}", HandlerFunc: InspectMoon,
	},
	Route{
		Name: "StatsMoon", Method: http.MethodGet, Pattern: "/moon/{id}/stats", HandlerFunc: StatsMoon,
	},

	Route{
		Name: "GenerateMoonID", Method: http.MethodGet, Pattern: "/tools/moon_id", HandlerFunc: GenMoonID,
	},
	Route{
		Name: "GenerateMoonID", Method: http.MethodGet, Pattern: "/tools/moon_id/{amount:[0-9]+}", HandlerFunc: GenMoonID,
	},

	Route{
		Name: "ListZone", Method: http.MethodGet, Pattern: "/zone", HandlerFunc: ListZone,
	},
	Route{
		Name: "CreateZone", Method: http.MethodPost, Pattern: "/zone/create", HandlerFunc: CreateZone,
	},
	Route{
		Name: "InspectZone", Method: http.MethodGet, Pattern: "/zone/{id}", HandlerFunc: InspectZone,
	},
	Route{
		Name: "RemoveZone", Method: http.MethodDelete, Pattern: "/zone/{id}", HandlerFunc: RemoveZone,
	},
	Route{
		Name: "AddMoonToZone", Method: http.MethodPost, Pattern: "/zone/{id}/add", HandlerFunc: AddMoonToZone,
	},
	Route{
		Name: "RemoveMoonFromZone", Method: http.MethodPost, Pattern: "/zone/{id}/remove", HandlerFunc: RemoveMoonFromZone,
	},
	Route{
		Name: "AddZonefile", Method: http.MethodPost, Pattern: "/zone/{id}/zonefile/add", HandlerFunc: AddZonefile,
	},
	Route{
		Name: "ListZonefile", Method: http.MethodGet, Pattern: "/zone/{id}/zonefile", HandlerFunc: ListZonefile,
	},
	Route{
		Name: "GetZonefile", Method: http.MethodGet, Pattern: "/zone/{id}/zonefile/{version:[0-9]+}", HandlerFunc: GetZonefile,
	},
	Route{
		Name: "DeployZonefile", Method: http.MethodPost, Pattern: "/zone/{id}/zonefile/{version:[0-9]+}/deploy", HandlerFunc: DeployZonefile,
	},

	Route{
		Name: "Events", Method: http.MethodGet, Pattern: "/events", HandlerFunc: ListEvents,
	},
	Route{
		Name: "Version", Method: http.MethodGet, Pattern: "/version", HandlerFunc: Version,
	},
	Route{
		Name: "SystemStats", Method: http.MethodGet, Pattern: "/stats", HandlerFunc: SystemStats,
	},
}

func NewRouter() *mux.Router {
	r := mux.NewRouter().StrictSlash(true)
	for _, route := range routes {
		r.Methods(route.Method).Name(route.Name).Path(route.Pattern).HandlerFunc(route.HandlerFunc)
	}
	// 注册websocket
	r.HandleFunc("/moon/{id}/ssh", SSH)
	return r
}
